package com.android.functionmanagement.receiver;

import java.text.SimpleDateFormat;
import java.util.Locale;

import com.android.functionmanagement.globle.ConstantGloble;
import com.android.functionmanagement.util.SharedPreferencesUtils;

import android.app.AlarmManager;
import android.content.BroadcastReceiver;
import android.content.Context;
import android.content.SharedPreferences;
import android.content.Intent;
import android.util.Log;

public class DateChangeReceiver extends BroadcastReceiver {
	AlarmManager am = null;

	@Override
	public void onReceive(Context context, Intent intent) {
		String action = intent.getAction();
		Log.e("yantm", " DateChangeReceiver action = " + action);
		if (action.equals(Intent.ACTION_BOOT_COMPLETED)) {
			SharedPreferencesUtils.setParam(context,
					ConstantGloble.STEP_LAST_STEP, 0);
			final SharedPreferences sp = context.getSharedPreferences(
					ConstantGloble.SHARED_PREF_FILE_NAME, Context.MODE_PRIVATE);
			String todayTime = sp.getString(ConstantGloble.STEP_TODAY_TIME, "");
			if (!getCurrentDate().equals(todayTime)) {
				SharedPreferencesUtils.setParam(context,
						ConstantGloble.STEP_TODAY_STEP, 0);
			}
			SharedPreferencesUtils.setParam(context,
					ConstantGloble.STEP_TODAY_TIME, getCurrentDate());
		} else if (action.equals("android.intent.action.ACTION_SHUTDOWN")) {
			Intent saveStepIt = new Intent(
					"android.intent.action.PEDOMETER_SAVE_STEPS");
			context.sendBroadcast(saveStepIt);
		} else if (action.equals(Intent.ACTION_DATE_CHANGED)) {
			Intent saveStepIt = new Intent(
					"android.intent.action.PEDOMETER_DATE_CHANGE");
			context.sendBroadcast(saveStepIt);
		}
	}

	SimpleDateFormat df = new SimpleDateFormat("yyyy-MM-dd",
			Locale.getDefault());

	public String getCurrentDate() {
		return df.format(System.currentTimeMillis());
	}

}
